<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use \think\Request;
use think\facade\View;
use app\common\model\GoodsList;
use app\common\model\Goods;
use app\common\model\Shang;
use app\common\model\Climb;

class ClimbList extends Base
{

    /**
     * 奖品列表
     */
    public function index(Request $request)
    {
        $keyword = $request->param('name');
        $status = $request->param('status');
        $start_time = $request->param('start_time');
        $end_time = $request->param('end_time');
        $goods_id = $request->param('goods_id');
        $num = $request->param('num/d');
        $prize_type = $request->param('prize_type/d');
        $whe = [];
        if (!empty($end_time)) {
            $end_time = strtotime($end_time) + 86399;
        } else {
            $end_time = time();
        }
        if (!empty($start_time)) {
            $start_time = strtotime($start_time);
            if ($start_time > $end_time) {
                return $this->renderError("时间填写错误");
            }
            $whe[] = ['addtime', 'BETWEEN', [$start_time, $end_time]];
        } else {
            $whe[] = ['addtime', '<', $end_time];
        }
        if (!empty($keyword)) {
            $whe['title'] = array('like', '%' . $keyword . '%');
        }
        $whe[] = ['status', '=', '1'];

        $whe[] = ['goods_id', '=', $goods_id];
        $whe[] = ['num', '=', $num];
        $field = '*';
        $order = 'sort asc,id desc';
        $data = GoodsList::getList($whe, $field, $order, $this->page);
        $whe[] = ['prize_type', '<', 5];
        $nums = GoodsList::where($whe)->sum('gailv');
        foreach ($data['list'] as &$v) {
            $v['shang'] = Shang::where(['id' => $v['shang_id']])->value('title');
            if ($nums > 0 && $v['prize_type'] != 5) {
                $v['num_rate'] = jisuan(jisuan($v['gailv'], $nums, '/', 5), 100, '*', 3);
            } else {
                $v['num_rate'] = 0;
            }
        }
        View::assign('list', $data['list']);
        View::assign('count', $data['count']);
        View::assign('page', $data['page']);
        View::assign('goods_id', $goods_id);
        View::assign('num', $num);
        View::assign('prize_type', $prize_type);
        return View::fetch('ClimbList/index');
    }

    /**
     * 关卡列表
     */
    public function climb(Request $request)
    {
        $goods_id = $request->param('goods_id/d');
        $w = [];
        $w[] = ['goods_id', '=', $goods_id];
        $data = climb::getAllList($w);
        View::assign('goods_id', $goods_id);
        View::assign('data', $data);
        return View::fetch('ClimbList/climb');
    }

    /**
     * 关卡编辑
     */
    public function climb_edit(Request $request)
    {
        if (!$request->isPost()) {
            $id = $request->param('id');
            $w = [];
            $w[] = ['id', '=', $id];
            $data = climb::getInfo($w, '*');
            View::assign("data", $data);
            return View::fetch("ClimbList/climb_edit");
        } else {
            $data = input('post.');
            if (empty($data['title'])) {
                return $this->renderError("请输入关卡名称");
            }
            if (empty($data['price'])) {
                return $this->renderError("请输入关卡价格");
            }
            $data['update_time'] = time();
            $dd = climb::where(['id' => $data['id']])->update($data);
            if ($dd) {
                return $this->renderSuccess('编辑成功');
            } else {
                return $this->renderError('编辑失败');
            }
        }
    }

    /*
     * 商品添加
     */
    public function add(Request $request)
    {
        if (!$request->isPost()) {
            $goods_id = $request->param('goods_id/d');
            View::assign('goods_id', $goods_id);
            $num = $request->param('num/d');
            if (empty($num)) {
                $num = 'end';
            }
            View::assign('num', $num);
            //获取赏
            $whe = [];
            $whe[] = ['id', 'between', [35,38]];
            $shang = Shang::getAllList($whe, '*', 'id asc');
            View::assign('shang', $shang);
            return View::fetch('ClimbList/add');
        } else {
            $data = input('post.');
            if (empty($data['title'])) {
                return $this->renderError('请输入名称');
            }
            if (empty($data['shang_id'])) {
                return $this->renderError('请选择赏类型');
            }
            if (empty($data['prize_type'])) {
                return $this->renderError('请选择奖品类型');
            }
            if (empty($data['price'])) {
                return $this->renderError('请输入价格');
            }
            if ($data['goods_type'] == 2) {
                if(empty($data['sale_time'])) {
                    return $this->renderError('请选择预售时间');
                }
                $data['sale_time'] = strtotime($data['sale_time']);
                if ($data['sale_time'] <= time()) {
                    return $this->renderError('预售时间选择错误');
                }
            }
            if ($data['num'] == 1 && $data['prize_type'] ==3) {
                return $this->renderError('第一层无需添加降级商品');
            }
            $arr['title'] = $data['title'];
            $arr['shang_id'] = $data['shang_id'];
            $arr['price'] = $data['price'];
            $arr['num'] = $data['num'];
            $arr['money'] = $data['money'];
            $arr['prize_code'] = getPrizeCode() . '_' . time();
            $arr['stock'] = 0;
            $arr['gailv'] = 0;
            $arr['imgurl'] = $data['imgurl'];
            $arr['goods_type'] = $data['goods_type'];
            $arr['goods_id'] = $data['goods_id'];
            $arr['sort'] = $data['sort'] ?? 1;
            $arr['prize_type'] = $data['prize_type'];
            $arr['status'] = 1;
            $arr['addtime'] = time();
            $dd = GoodsList::insertGetId($arr);
            if ($dd) {
                return $this->renderSuccess('添加成功');
            } else {
                return $this->renderError('添加失败');
            }
        }
    }

    /*
 * 商品编辑
 */
    public function edit(Request $request)
    {
        if (!$request->isPost()) {
            //查询盲盒详情
            $id = $request->param('id/d');
            $data = GoodsList::getInfo(['id' => $id], '*');
            View::assign("data", $data);
            //获取赏
            $whe = [];
            $whe[] = ['id', 'between', [35,38]];
            $shang = Shang::getAllList($whe);
            View::assign('shang', $shang);
            return View::fetch('ClimbList/edit');
        } else {
            $data = input('post.');
            if (empty($data['title'])) {
                return $this->renderError('请输入名称');
            }
            if (empty($data['shang_id'])) {
                return $this->renderError('请选择赏类型');
            }
            if (empty($data['prize_type'])) {
                return $this->renderError('请选择奖品类型');
            }
            if (empty($data['price'])) {
                return $this->renderError('请输入时长价');
            }
            if ($data['goods_type'] == 2) {
                if(empty($data['sale_time'])) {
                    return $this->renderError('请选择预售时间');
                }
                $data['sale_time'] = strtotime($data['sale_time']);
                if ($data['sale_time'] <= time()) {
                    return $this->renderError('预售时间选择错误');
                }
            }
            // if ($data['num'] == 1 && $data['prize_type'] ==3) {
            //     return $this->renderError('第一层无需添加降级商品');
            // }
            // if ($data['num'] < 10 && $data['prize_type'] ==5) {
            //     $ccc = GoodsList::where(['prize_type' => $data['prize_type'], 'num' =>$data['num']])->find();
            //     if (!empty($ccc)) {
            //         return $this->renderError('首通奖品一个就可以了');
            //     }
            // }
            $arr['title'] = $data['title'];
            $arr['shang_id'] = $data['shang_id'];
            $arr['price'] = $data['price'];
            $arr['goods_type'] = $data['goods_type'];
            $arr['money'] = $data['money'];
            $arr['imgurl'] = $data['imgurl'];
            $arr['sort'] = $data['sort'];
            $arr['prize_type'] = $data['prize_type'];
            $dd = GoodsList::where(['id' => $data['id']])->update($arr);
            if ($dd) {
                return $this->renderSuccess('编辑成功');
            } else {
                return $this->renderError('编辑失败');
            }
        }
    }

    /**
     * 操作
     */
    public function cao(Request $request)
    {
        $id = $request->post('id/d');
        $status = $request->post('type/d');
        if (!in_array($status, array(2, 3, 1))) {
            return $this->renderError("数据错误");
        }
        $data = GoodsList::where(['id' => $id])->field('id,title')->find();
        if (!$data) {
            return $this->renderError("数据不存在");
        }
        $result = GoodsList::where(['id' => $id])->update(["status" => $status]);
        if ($result) {
            return $this->renderSuccess("操作成功");
        } else {
            return $this->renderError("网络繁忙，请稍后");
        }
    }

    /**
     * 增加概率
     * @param Request $request [description]
     * @return [type]           [description]
     */
    public function gailv(Request $request)
    {
        $id = $request->post('id/d');
        $num = $request->post('num/d');
        if ($num < 0) {
            return $this->renderError("概率不能为负数");
        }
        if ($num == 0) {
            return $this->renderError("概率不能为零");
        }
        $dd = GoodsList::where(['id' => $id])->update(["gailv" => $num, 'stock' => $num]);
        if ($dd) {
            return $this->renderSuccess("修改成功");
        } else {
            return $this->renderError("网络繁忙，请稍后");
        }
    }
}